home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Telnet 2.5.src.ThinkC / source / NCSA Driver / croft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-14  |  8.3 KB  |  388 lines  |  [TEXT/MPS ]

  1. #ifndef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    croft.c
  6. *     by Gaige B. Paulsen
  7. ****************************************************************************
  8. *                                                                          *
  9. *      Uses    :                                                               *
  10. *      TCP/IP kernel for NCSA Telnet                                       *
  11. *      by Tim Krauskopf                                                    *
  12. *       with Macintosh code by Gaige B. Paulsen                                 *
  13. *                                                                          *
  14. *      National Center for Supercomputing Applications                     *
  15. *      152 Computing Applications Building                                 *
  16. *      605 E. Springfield Ave.                                             *
  17. *      Champaign, IL  61820                                                *
  18. *                                                                          *
  19. *                                                                          *
  20. ****************************************************************************
  21. *
  22. *    KIP/Croft gateway handling code.    
  23. *
  24. *    Called by:
  25. *        maclook.c
  26. *        mactools.c
  27. *        macutil.c
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. #include <AppleTalk.h>
  34. #include <Dialogs.h>
  35. #include <Memory.h>
  36. /*#include <Strings.h>    /* BYU LSC */
  37.  
  38. #include "configrec.h"
  39. #include "kip.h"
  40. #include "maclook.h"
  41. #include "mpw.h"
  42.  
  43. extern void OtherError();    /* BYU */
  44.  
  45. #ifdef OLDM
  46. struct NBPentadd {
  47.     AddrBlock addr;
  48.     EntityName entity;
  49.     };
  50. typedef struct NBPentadd NBPentadd;
  51.  
  52. NBPRecHdl
  53.     myRecH;            /* Handle used for AB records */
  54. #endif OLDM
  55.  
  56. char
  57.     KIPzone[33]="\p*",    /* Kip zone in P-string */
  58.     *KIPnameptr=0L;    /* Pointer to current name block (NBP) */
  59.  
  60. void KIPunregister
  61.   (
  62.     void
  63.   )
  64. {
  65.     MPPParamBlock Mpb;
  66.     int i;
  67.     char temp[50];
  68.     
  69.     Mpb.NBPentityPtr = &KIPnameptr[9];
  70.     i =PRemoveName(&Mpb, FALSE);
  71.     sprintf(temp, "RN=%d",i);
  72.     putln(temp);
  73. }
  74.  
  75. #ifdef DEBUGG
  76.  
  77. /*
  78.  *    NBPdisplay- display an NBP packet (eap) in a reasonable format 
  79.  *
  80.  */
  81.  
  82. NBPdisplay( eap)
  83. NTElement *eap;
  84. {
  85.     unsigned char *cp;
  86.     int len1,len2;
  87.  
  88.     cp=(char *)eap+3;
  89.     len1= cp[1];
  90.     len2= cp[ len1+2];
  91.  
  92.     printf("Name: %*s\015Type: %*s\015Zone: *\015Atalk Address: %d.%d.%d\015",    /* BYU 2.4.18 - changed \n to \015 */
  93.         len1,&cp[2], len2, &cp[ len1+3],
  94.         eap->nteAddress.aNet, eap->nteAddress.aNode, eap->nteAddress.aSocket);
  95. }
  96. #endif DEBUGG
  97.  
  98. /*
  99.  *    NBPfind - check for <object,typename,zone> wikth the resultant address
  100.  *            in buffer which is bufsize bytes.  Expect expect responses.
  101.  *
  102.  *    returns <0 if NBP/network error
  103.  *            =0 if not found
  104.  *            >0 if ok
  105.  */
  106.  
  107. int NBPfind( object,typename,zone, buffer,bufsize, expect,rti)
  108. Str32 *object, *typename, *zone;
  109. char *buffer;
  110. int rti,expect,bufsize;
  111. {
  112.     MPPParamBlock Mpb;
  113.     EntityName entity;
  114.     int err;
  115.  
  116.     NBPSetEntity((Ptr) &entity, (Ptr) object, (Ptr) typename, (Ptr) zone);
  117.  
  118.     Mpb.NBPentityPtr = (Ptr) &entity;
  119.     Mpb.NBPretBuffPtr=buffer;
  120.     Mpb.NBPretBuffSize=bufsize;
  121.     Mpb.NBPmaxToGet=expect;
  122.     Mpb.NBPinterval= rti;
  123.     Mpb.NBPcount   = 4;    
  124.     err= PLookupName(&Mpb, FALSE);
  125.     if (err!=0) {
  126.         putln("Error in NBP Lookup.....%d\015");    /* BYU 2.4.18 - changed \n to \015 */
  127.         return(-1);
  128.         }
  129.     else return(Mpb.NBPnumGotten);
  130. }
  131.  
  132. /*
  133.  *    NBPregister - Register on the network as <object,typename,zone> using
  134.  *            buffer of size to store permanent information.
  135.  *
  136.  *    returns <0 if NBP/network error
  137.  *            >=0 if ok
  138.  */
  139. int NBPregister( object,typename,zone, buffer,size )
  140. Str32 *object, *typename, *zone;
  141. NamesTableEntry *buffer;
  142. int size;
  143. {
  144. #pragma unused(size)
  145.     MPPParamBlock Mpb;
  146.     int err;
  147.     THz curzone;
  148.     
  149.     if (KIPnameptr)
  150.         KIPunregister();
  151.     curzone = GetZone();
  152.     SetZone(SystemZone());
  153.     buffer = (NamesTableEntry *) NewPtr((long)sizeof(NamesTableEntry));    /* BYU LSC */
  154.     KIPnameptr = (char *) buffer;        /* BYU LSC */
  155.     SetZone(curzone);
  156.  
  157.     NBPSetNTE((Ptr) buffer, (Ptr) object, (Ptr) typename, (Ptr) zone, 72);
  158.  
  159.     Mpb.NBPntQElPtr = (Ptr) buffer;
  160.     Mpb.NBPinterval= 5;
  161.     Mpb.NBPcount   = 5;    
  162.     Mpb.NBPverifyFlag=1;    
  163.  
  164.     err= PRegisterName(&Mpb, FALSE);
  165.     if (err!=0) {
  166.         putln("Error in NBP Register.....%d\015");    /* BYU 2.4.18 - changed \n to \015 */
  167.         DisposPtr((Ptr) buffer);
  168.         return(-1);
  169.         }
  170.     else return(Mpb.MPPioResult);
  171. }
  172.  
  173. /*
  174.  *    ATPsend - sends a request to the ATP server at addr using the packet in
  175.  *            buffer which is size bytes long puting the return data in the 
  176.  *            retdata BDS.
  177.  *
  178.  *    returns <0 if ATP/network error
  179.  *            >=0 if ok
  180.  */
  181.  
  182. ATPsend( addr, buffer, size, retdata)
  183. AddrBlock *addr;
  184. char *buffer;
  185. int size;
  186. BDSPtr retdata;
  187. {
  188.     ATPParamBlock Apb;
  189.     int err;
  190.  
  191.     Apb.ATPaddrBlock=*addr;
  192.     Apb.ATPreqLength=size;
  193.     Apb.ATPreqPointer=buffer;
  194.     Apb.ATPbdsPointer = (Ptr) retdata;
  195.     Apb.ATPatpFlags=0;
  196.     Apb.ATPtimeOutVal=5;
  197.     Apb.ATPretryCount=4;
  198.     Apb.ATPnumOfBuffs=1;
  199.  
  200.     err=PSendRequest( &Apb, FALSE);
  201.  
  202.     return(err);
  203. }
  204.  
  205. /*
  206.  *    KIParp - Use the croft code to arp for machine with the IP number ipnum,
  207.  *        returning the AT address in addrloc.
  208.  *
  209.  *    returns <0 if NBP/network error
  210.  *            >=0 if ok
  211.  */
  212.  
  213. int KIParp
  214.   (
  215.     unsigned char ipnum[4],
  216.     AddrBlock *addrloc
  217.   )
  218. {
  219.     NTElement retbuff;
  220.     int ret;
  221.     char temps[20];
  222.  
  223.     sprintf(temps," %d.%d.%d.%d", (int)ipnum[0],(int)ipnum[1],(int)ipnum[2],
  224.             (int)ipnum[3]);
  225.  
  226. #ifdef DEBUGG
  227.     puts(temps); puts(" is being looked up\015");    /* BYU 2.4.18 - changed \n to \015 */
  228. #endif
  229.  
  230.     temps[0]=strlen(temps)-1;
  231.     ret=NBPfind((Str32 *) temps, (Str32 *) "\pIPADDRESS", (Str32 *) KIPzone, (char *) &retbuff,sizeof(retbuff),1,40);    /* BYU LSC */
  232.  
  233. #ifdef DEBUGG
  234.     NBPdisplay(&retbuff);
  235. #endif DEBUGG
  236.  
  237.     *addrloc=retbuff.nteAddress;
  238.     return(ret);
  239. }
  240.  
  241. /*
  242.  *    KIPregister - Use the croft code to register our machine on the network as
  243.  *            ipno.
  244.  *
  245.  *    returns <0 if NBP/network error
  246.  *            >=0 if ok
  247.  */
  248.  
  249. int KIPregister
  250.   (
  251.     unsigned char *ipnum
  252.   )
  253. {
  254.     char temps[20];
  255.     int ret;
  256.  
  257.     sprintf(temps," %d.%d.%d.%d", (int)ipnum[0],(int)ipnum[1],(int)ipnum[2],
  258.             (int)ipnum[3]);
  259. #ifdef DEBUGG
  260.     puts(temps); puts(" is being registered \015");        /* BYU 2.4.18 - changed \n to \015 */
  261. #endif
  262. #ifdef DONTDOTHISRIGHTNOW
  263.     KIPnameptr=NewPtr(110);
  264.     if (KIPnameptr==0L) return(-1);
  265. #endif
  266.     temps[0]=strlen(temps)-1;
  267.     ret=NBPregister((Str32 *) temps, (Str32 *) "\pIPADDRESS", (Str32 *) KIPzone , (NamesTableEntry *) KIPnameptr,110);    /* BYU LSC */
  268.     return(ret);
  269. }
  270.  
  271. /*
  272.  *    KIPgetdynam - Use the croft code to get a dynamic IP number for us from
  273.  *            the server at servAddr. The IP number is stored in IPaddr.
  274.  *
  275.  *    returns <0 if NBP/network error
  276.  *            >=0 if ok
  277.  */
  278.  
  279. int KIPgetdynam
  280.   (
  281.     AddrBlock *servAddr,
  282.     long *IPaddr
  283.   )
  284. {
  285.     IPGP gateRec,gateSend;
  286.     BDSType retBDS;
  287.     int err;
  288.  
  289.     retBDS[0].buffSize=sizeof(gateRec);
  290.     retBDS[0].buffPtr = (Ptr) &gateRec;
  291.  
  292.     gateSend.opcode=ipgpAssign;
  293.  
  294.     err=ATPsend( servAddr, (char *) &gateSend, sizeof(gateSend), (BDSPtr) retBDS);    /* BYU LSC */
  295.  
  296.     if (err!=0) 
  297.         {
  298.         *IPaddr = 0L;
  299. #ifndef MPW
  300.         OtherError("\PUnable to get dynamic IP number","\P ");
  301. #else
  302.         OtherError("Unable to get dynamic IP number"," ");
  303. #endif MPW
  304.  
  305.         return(err);
  306.         }
  307.         
  308.     *IPaddr=gateRec.ipaddress;
  309. #ifdef DEBUGG
  310.     if (gateRec.opcode<0) puts(gateRec.string);
  311. #endif
  312.     return(0);
  313. }
  314.  
  315. /*
  316.  *    KIPgetns - Use the croft code to get the IP number of the Nameserver for us from
  317.  *            the server at servAddr. The IP number is stored in IPaddr.
  318.  *
  319.  *    returns <0 if NBP/network error
  320.  *            >=0 if ok
  321.  */
  322.  
  323. int KIPgetns
  324.   (
  325.     AddrBlock *servAddr,
  326.     long *IPaddr
  327.   )
  328. {
  329.     IPGP gateRec,gateSend;
  330.     BDSType retBDS;
  331.     int err;
  332.  
  333.     retBDS[0].buffSize=sizeof(gateRec);
  334.     retBDS[0].buffPtr = (Ptr) &gateRec;
  335.  
  336.     gateSend.opcode=ipgpServer;
  337.  
  338.     err=ATPsend( servAddr, (char *) &gateSend,sizeof(gateSend), (BDSPtr) retBDS);    /* BYU LSC */
  339.  
  340.     if (err!=0) return(err);
  341.     *IPaddr=gateRec.ipname;
  342. #ifdef DEBUGG
  343.     if (gateRec.opcode<0) puts(gateRec.string);
  344. #endif
  345.     return(0);
  346. }
  347.  
  348. /*
  349.  *    KIPfindgate - Use the croft code to find a gateway to use.  Returns the
  350.  *            AT address in addr.
  351.  *
  352.  *    returns <0 if NBP/network error
  353.  *            =0 if there are none.
  354.  *            >0 if ok
  355.  */
  356.  
  357. int KIPfindgate
  358.   (
  359.     AddrBlock *addr
  360.   )
  361. {
  362.     NTElement retbuff;
  363.     int ret;
  364.  
  365.     ret=NBPfind( (Str32 *) "\p=", (Str32 *) "\pIPGATEWAY", (Str32 *) KIPzone, (char *) &retbuff, sizeof(retbuff),1,3);    /* BYU LSC */
  366.  
  367. #ifdef DEBUGG
  368.     NBPdisplay(&retbuff);
  369. #endif DEBUGG
  370.  
  371.     *addr=retbuff.nteAddress;
  372.     return(ret);
  373. }
  374.  
  375. /* KIPsetzone - set up KIPzone */
  376.  
  377. void KIPsetzone
  378.   (
  379.     char *zone
  380.   )
  381. {
  382.     strncpy(KIPzone,zone,32);        /* Copy incoming */
  383.     KIPzone[32]=0;                    /* NUL terminate */
  384.     c2pstr(KIPzone);                /* Convert to pascal string */
  385.     putln("new zone is:");
  386.     putln(zone);
  387. }
  388.